package com.wbpay.sdk.http.impl;

import android.content.Context;

import com.wbpay.sdk.http.HttpCallback;
import com.wbpay.sdk.http.IHttp;
import com.wbpay.sdk.utils.Log;
import com.wbpay.sdk.utils.UrlHelper;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * HTTP实现类
 */

public class HttpImpl implements IHttp {
    private Context mContext;
    private OkHttpClient mOkHttpClient;

    public HttpImpl(Context context) {
        mContext = context;
        mOkHttpClient = new OkHttpClient();
    }

    @Override
    public void get(String url, Map<String, String> params,
                    Map<String, String> headers, final HttpCallback callback) {
        url = UrlHelper.addQueryParams(mContext, url, params);
        Request.Builder builder = new Request.Builder();
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        }
        final Request request = builder.url(url).build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                Headers headers = null;
                String data = null;
                try {
                    headers = response.headers();
                    data = response.body().string();
                } catch (IOException e) {
                    Log.d(Log.TAG, "error : " + e);
                }
                if (callback != null) {
                    callback.onSuccess(data);
                }
            }

            @Override
            public void onFailure(Call call, final IOException e) {
                if (callback != null) {
                    callback.onFailure(-1, String.valueOf(e));
                }
            }
        });
    }

    @Override
    public void post(String url, Map<String, String> params, Map<String, String> headers, final HttpCallback callback) {
        // url = UrlHelper.addQueryParams(mContext, url, params);
        Request.Builder builder = new Request.Builder();
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        }

        FormBody.Builder bodyBuilder = new FormBody.Builder();
        if (params != null) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                bodyBuilder.add(entry.getKey(), entry.getValue());
            }
        }
        RequestBody formBody = bodyBuilder.build();
        final Request request = builder.url(url).post(formBody).build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                Headers headers = null;
                String data = null;
                try {
                    headers = response.headers();
                    data = response.body().string();
                } catch (IOException e) {
                    Log.d(Log.TAG, "error : " + e);
                }
                if (callback != null) {
                    callback.onSuccess(data);
                }
            }

            @Override
            public void onFailure(Call call, final IOException e) {
                if (callback != null) {
                    callback.onFailure(-1, String.valueOf(e));
                }
            }
        });
    }

    @Override
    public void post(String url, String body, String contentType, Map<String, String> headers, final HttpCallback callback) {
        Request.Builder builder = new Request.Builder();
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        }
        MediaType JSON = MediaType.parse(contentType + "; CHARSET=UTF-8");
        RequestBody requestBody = RequestBody.create(JSON, body);
        final Request request = builder.url(url).post(requestBody).build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                Headers headers = null;
                String data = null;
                try {
                    headers = response.headers();
                    data = response.body().string();
                } catch (IOException e) {
                    Log.d(Log.TAG, "error : " + e);
                }
                if (callback != null) {
                    callback.onSuccess(data);
                }
            }

            @Override
            public void onFailure(Call call, final IOException e) {
                if (callback != null) {
                    callback.onFailure(-1, String.valueOf(e));
                }
            }
        });
    }
}
